import { ElMessage } from 'element-plus'

// 验证核销数据
export function checkInventory(selectData: any = []) {
  console.log('xxxx')
  // 批量核销
  if (selectData.length < 2) {
    ElMessage({
      showClose: true,
      message: '至少选择俩条数据',
      type: 'warning'
    })
  } else {
    // 批次号
    const batchNumberType = selectData.every(
      (target: any) => target.batchNumber === selectData[0].batchNumber
    )
    // 序号
    const partnumberType = selectData.every(
      (target: any) => target.partnumber === selectData[0].partnumber
    )
    // 件号
    const toolIdType = selectData.every((target: any) => target.toolId === selectData[0].toolId)
    if (batchNumberType) {
      if (partnumberType) {
        if (toolIdType) {
          // 请求接口
          const arr: any = []
          let lossNumber: Number = 0
          let profitNumber: Number = 0
          selectData.forEach((item: any) => {
            arr.push(item.id)
            if (item.inventoryConclusion == '盘亏') {
              lossNumber = Number(lossNumber) + Number(item.count)
            } else if (item.inventoryConclusion == '盘盈') {
              profitNumber = Number(profitNumber) + Number(item.count)
            }
          })
          console.log(lossNumber, profitNumber)
          if (lossNumber === profitNumber) {
            return true
          } else {
            ElMessage({
              showClose: true,
              message: '请核对数量',
              type: 'error'
            })
          }
        } else {
          ElMessage({
            showClose: true,
            message: '件号必须相同',
            type: 'warning'
          })
        }
      } else {
        ElMessage({
          showClose: true,
          message: '序号必须相同',
          type: 'warning'
        })
      }
    } else {
      ElMessage({
        showClose: true,
        message: '批次号必须相同',
        type: 'warning'
      })
    }
  }
}

export default {
  checkInventory
}
